<template>
  <teleport to="body">
    <div class="menu-box" :style="boxStyle">
      <div ref="chartDom" class="chart" v-show="store.showUi"></div>
      <div
        v-show="store.showUi"
        :class="['menu-item', {'menu-action': currentMenu === item.name}]"
        :style="menuStyle(index)"
        v-for="(item, index) in menuData"
        :key="item"
        @click="onClickMenu(item, index)"
      >
        <SvgIcon class="menu-icon center" :icon="item.icon"></SvgIcon>
        <div v-if="currentMenu === item.name" class="menu-name">{{currentMenu}}</div>
      </div>
      <div class="menu-center center menu-center_bg" v-show="store.showUi"></div>
      <div
        class="menu-center center menu-center_content"
        @mouseover="menuShow = true"
        @mouseleave="menuShow = false"
        @click="onChangeUi"
        v-show="store.showUi"
      >
        <div class="btn-hide flex-center" v-show="menuShow">
          <SvgIcon class="menu-icon" icon="hide"></SvgIcon>
          <span style="font-size: 12px">隐藏UI</span>
        </div>
      </div>
      <SvgIcon class="btn-show" icon="show" @click="onChangeUi" v-show="!store.showUi"></SvgIcon>
    </div>
  </teleport>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, computed, onMounted, defineProps, defineEmits } from "vue";
import SvgIcon from "../../../components/SvgIcon.vue";
import { useStore } from "../../../store/store";

const store = useStore();
const emit = defineEmits(["changeMenu"]);
const props = defineProps({
  menuData: {
    type: Array,
    default: []
  },
  currentMenu: {
    type: String,
    default: ""
  }
});

// 大小
const size = {
  box: 200, // 刻度盘大小
  menu: 40 // 菜单大小
};

const boxStyle = computed(() => {
  const s = size.box + "px";
  return {
    width: s,
    height: s,
    transform: store.showUi ? "scale(1, 1)" : "scale(0.1, 0.1)",
    transformOrigin: "0% 100%"
  };
});

//中心点偏移量
const centerOffset = (size.box - size.menu) / 2;
const menuStyle = computed(() => index => {
  const avd =
    props.menuData.length === 1
      ? 0
      : (180 / (props.menuData.length - 1)) * index;
  const ahd = (avd * 2 * Math.PI) / 360;
  const radius = size.box / 2 + 25;
  return {
    left: Math.sin(ahd) * radius + centerOffset + "px",
    bottom: Math.cos(ahd) * radius + centerOffset + "px",
    width: size.menu + "px",
    height: size.menu + "px"
  };
});
const chartDom = ref(null);
let echart = null;
let menuShow = ref(false);
function onClickMenu(menu, index) {
  const obj = {
    data: [
      {
        value:
          props.menuData.length === 1
            ? 0
            : (100 / (props.menuData.length - 1)) * index
      }
    ]
  };
  echart.setOption({
    series: [obj, obj]
  });
  emit("changeMenu", menu);
}
function onChangeUi(data) {
  store.showUi = !store.showUi;
}
// function setEchart
onMounted(() => {
  echart = echarts.init(chartDom.value);
  var option;

  option = {
    // animationDuration: 100,
    series: [
      {
        type: "gauge",
        center: ["50%", "50%"],
        startAngle: -90,
        radius: "75%",
        endAngle: 270,
        min: 0,
        max: 200,
        itemStyle: {
          color: "rgba(0, 255, 255, 0.8)"
        },
        progress: {
          show: true,
          roundCap: true,
          width: 13
        },
        pointer: {
          show: false
        },
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          show: false
        },
        detail: {
          show: false
        },
        data: [
          {
            value: 0
          }
        ]
      },
      {
        type: "gauge",
        center: ["50%", "50%"],
        min: 0,
        max: 200,
        startAngle: 90,
        endAngle: 450,
        radius: "90%",
        splitNumber: 10,
        itemStyle: {
          color: "rgba(0, 255, 255, 0.8)"
        },
        progress: {
          show: true,
          roundCap: true,
          width: 15
        },
        pointer: {
          show: false
        },
        axisLine: {
          lineStyle: {
            width: 10,
            opacity: 0
          }
        },
        axisLabel: {
          show: false
        },
        axisTick: {
          distance: size.box - 65,
          splitNumber: 10,
          length: 4,
          lineStyle: {
            width: 1,
            color: "#999"
          }
        },
        splitLine: {
          distance: size.box - 70,
          length: 5,
          lineStyle: {
            width: 2,
            color: "#999"
          }
        },
        title: {
          show: false
        },
        detail: {
          show: false
        },
        data: [
          {
            value: 0
          }
        ]
      },
      {
        type: "gauge",
        center: ["50%", "50%"],
        startAngle: 0,
        endAngle: 360,
        radius: "70%",
        min: 0,
        max: 100,
        itemStyle: {
          color: "#0a9196"
        },
        progress: {
          show: true,
          // roundCap: true,
          width: 2
        },
        pointer: {
          show: false
        },
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          show: false
        },
        detail: {
          show: false
        },
        data: [
          {
            value: 100
          }
        ]
      },
      {
        type: "gauge",
        center: ["50%", "50%"],
        startAngle: 0,
        endAngle: 360,
        radius: "85%",
        min: 0,
        max: 100,
        itemStyle: {
          color: "#0a9196"
        },
        progress: {
          show: true,
          // roundCap: true,
          width: 2
        },
        pointer: {
          show: false
        },
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        splitLine: {
          show: false
        },
        axisLabel: {
          show: false
        },
        detail: {
          show: false
        },
        data: [
          {
            value: 100
          }
        ]
      }
    ]
  };

  option && echart.setOption(option);
});
</script>

<style lang="scss" scoped>
.menu-box {
  position: absolute;
  bottom: 100px;
  left: 20px;
  z-index: 999;
  transition: all 0.5s;
  .menu-item {
    position: absolute;
    color: #fff;
    background-color: transparent;
    border: 1px solid #0a9196;
    border-radius: 12px;
    cursor: pointer;
    z-index: 200;
  }
  .menu-action {
    background-color: #0a9196;
    color: #00ffff;
  }
  .menu-icon {
    font-size: 22px;
  }
  .menu-name {
    width: 100px;
    position: absolute;
    top: 10px;
    left: 50px;
    font-size: 14px;
  }
  .menu-center {
    width: 140px;
    height: 140px;
    border-radius: 50%;
  }
  .menu-center_bg {
    background: radial-gradient(#00ffff, #000000, #000000);
  }
  .btn-hide {
    color: #fff;
  }
  .menu-center_content {
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
    cursor: pointer;
    overflow: hidden;
  }
  .menu-center_content:hover {
    background-color: rgba(0, 0, 0, 0.3);
  }
  .chart {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100;
  }
}
.btn-show {
  position: absolute;
  // background: radial-gradient(#00ffff, #000000, #000000);
  width: 100%;
  height: 100%;
  bottom: 0px;
  left: 0px;
  border-radius: 50%;
  color: #fff;
  cursor: pointer;
}
.center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.flex-center {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btn-group {
  width: 100%;
  height: 100%;
}
</style>